package com.medeiros.upmoney.activity;

import java.math.BigDecimal;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.medeiros.upmoney.data.LimiteDataProvider;
import com.medeiros.upmoney.model.Limite;
import com.medeiros.upmoney.util.ViewUtil;

public class ConfiguracaoLimite extends Activity {

	private Limite original;
	LinearLayout layout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = this.getIntent();
		original = (Limite) intent.getSerializableExtra("limite");

		LayoutInflater inflater = getLayoutInflater(); 
		layout =	(LinearLayout) inflater.inflate(R.layout.limitconfig, null);
		setContentView(layout);

		initValues();
		registrarBotoes();
	}

	private void initValues() {
		TextView categoria = (TextView) this.findViewById(R.id.tituloLimite);
		categoria.setText(original.getCategoria().getNome());

		EditText editValor = (EditText) this.findViewById(R.id.valorLimiteEdit);
		editValor.setText(String.valueOf(original.getValor().doubleValue()));
		
		EditText editGasto = (EditText) this.findViewById(R.id.gastoLimiteEdit);
		editGasto.setText(String.valueOf(original.getGasto().doubleValue()));
		
	}

	private void registrarBotoes() {
		OnClickListener listenter = new OnClickListener() {
			public void onClick(View v) {
				ConfiguracaoLimite.this.confirmar();
			}
		};
		ViewUtil.registrarClickListener(this, listenter, R.id.confirmar);
	}

	protected void cancelar() {
		this.finish();
	}

	protected void confirmar() {
		setValorLimite();
		setGastoLimite();
		LimiteDataProvider.getInstance().write(this,  original);
		this.finish();
	}

	private void setValorLimite() {
		EditText editValor = (EditText) this.findViewById(R.id.valorLimiteEdit);
		String text = editValor.getText().toString();
		double valor = Double.parseDouble(text);
		this.original.setValor(new BigDecimal(valor));
	}

	private void setGastoLimite() {
		EditText editGasto = (EditText) this.findViewById(R.id.gastoLimiteEdit);
		String text = editGasto.getText().toString();
		double valor = Double.parseDouble(text);
		this.original.setGasto(new BigDecimal(valor));
	}
}
